var wage_detail_name, wagestatus, oz_department, flag = true;
var wage_detail_name;
var dnme;
var money = /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
var path = $("#path").val();

function Mesload() {
	$('#table')
			.bootstrapTable(
					{
						method : "POST",
						url : "cp_payoff_index.json",
						striped : true,
						singleSelect : false,
						dataType : "json",
						pagination : true, // 分页
						pageSize : 10,
						pageNumber : 1,
						search : false, // 显示搜索框
						contentType : "application/x-www-form-urlencoded",
						queryParams : null,
						columns : [
								{
									checkbox : "true",
									field : 'id',
									align : 'center',
									valign : 'middle'
								},
								{
									title : '薪酬单编码',
									field : 'wage_detail_name',
									align : 'center'

								},
								{
									title : '薪酬单状态',
									field : 'wage_status_name',
									align : 'center'

								},

								{
									title : "I级机构名称",
									field : 'oz_department',
									align : 'center',
									valign : 'middle',

								},
								{
									title : 'II级机构名称',
									field : 'p_classname',
									align : 'center',
									valign : 'middle'
								},
								{
									title : '人数',
									field : 'countPersonnelfile_id',
									align : 'center',

								},

								{
									title : '基本薪酬总额',
									field : 'sumEm_totalmoney',
									align : 'center',
									valign : 'middle',

								},
								{
									title : '操作',
									field : '',
									align : 'center',

									formatter : function(value, row) {
										var detail1 = '<a href="#" onclick="detail1(\''
												+ row.wage_detail_name
												+ '\')">查看明细</a> '
										var register = '<a href="#" onclick="register(\''
												+ row.wage_detail_name
												+ '\')">登记</a> '

										if (row.wage_status_name == "已发"
												|| row.wage_status_name == "已登记") {
											return detail1;
										} else {
											return register;
										}
									}
								} ],
						onEditableSave : function(field, row, oldValue, $el) {
							var rows = {
								index : 1, // 更新列所在行的索引
								field : "sumEm_totalmoney", // 要更新列的field
								value : "INITIALIZATION" // 要更新列的数据
							}// 更新表格数据
							$('#table').bootstrapTable("updateCell", rows);
						}
					});

	getData1();

}

// 初始化薪酬状态下拉菜单
$.ajax({
	type : "POST",
	url : "getWageStatus.json",
	dataType : "json",
	success : function(data) {
		if (data != null) {
			var wageid = $("#wagestatus_id").val();
			$("#wagestatus").html("");
			var options = "<option value=\"0\">请选择</option>";
			for (var i = 0; i < data.length; i++) {
				if (wageid != null && wageid != undefined
						&& data[i].id == wageid) {
					options += "<option selected=\"selected\" value=\""
							+ data[i].id + "\" >" + data[i].wage_status_name
							+ "</option>";
				} else {
					options += "<option value=\"" + data[i].id + "\" >"
							+ data[i].wage_status_name + "</option>";
				}
			}
			$("#wagestatus").html(options);
		}
	}
});
// 初始化I级机构下拉菜单
$
		.ajax({
			type : "POST",
			url : "getOg_Departmentlist.json",
			dataType : "json",
			success : function(data) {
				if (data != null) {
					var depid = $("#oz_department_id").val();
					$("#oz_department").html("");
					var options = "<option value=\"0\">请选择</option>";
					for (var i = 0; i < data.length; i++) {
						if (depid != null && depid != undefined
								&& data[i].id == depid) {
							options += "<option selected=\"selected\" value=\""
									+ data[i].id + "\" >"
									+ data[i].oz_department + "</option>";
						} else {
							options += "<option value=\"" + data[i].id + "\" >"
									+ data[i].oz_department + "</option>";
						}
					}
					$("#oz_department").html(options);
				}
			}
		});
function getData1() {
	wage_detail_name = "";
	wagestatus = "";
	oz_department = "";
	wagestatus = $("#wagestatus").val();
	oz_department = $("#oz_department").val();
	$.ajax({
		type : "GET",
		url : "cp_payoff_index.json?wage_detail_name="
				+ $("#wage_detail_name").val() + "&wagestatus=" + wagestatus
				+ "&oz_department=" + oz_department,
		dataType : "json",
		success : function(result) {
			if (result != null) {
				var TableData = result;
				$('#table').bootstrapTable("load", TableData);
			}
		}
	})
}

function register(wage_detail_name) {
	wage_detail_name = wage_detail_name;
	$("#light").css("display", "inline");
	$("#fade").css("display", "inline");
	 $("#table2").css("display", "none");
	 $("#table1").css("display", "inline");
	 $("#btn").css("display", "inline");
	$.ajax({
		type : "GET",
		url : "cp_payoff_index.json?wage_detail_name=" + wage_detail_name,
		dataType : "json",
		success : function(data) {
			if (data != null) {

				$("#span_wage_detail_name").html(data[0].wage_detail_name);
				$("#span_oz_department").html(data[0].oz_department);
				$("#span_countPersonnelfile_id").html(
						data[0].countPersonnelfile_id);
				$("#span_sumEm_totalmoney").html(data[0].sumEm_totalmoney);
				$("#span_sendTotalMoney").html("");
			}
		}
	})
	getPayDate();
	$('#table1').bootstrapTable(
			{

				method : "POST",
				url : "cp_payoff_index_table.json",
				striped : true,
				singleSelect : false,
				dataType : "json",
				search : false, // 显示搜索框
				contentType : "application/x-www-form-urlencoded",
				queryParams : function(params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
					return {
						pageSize : params.limit, // 每页要显示的数据条数
						offset : params.offset, // 每页显示数据的开始行号
						sort : params.sort, // 要排序的字段
						sortOrder : params.order, // 排序规则
						wage_detail_name : wage_detail_name
					// 额外添加的参数
					}
				},

				columns : [ {
					title : '姓名',
					field : 'pf_name',
					align : 'center'
				}, {
					title : '档案编码',
					field : 'pid',
					align : 'center',
					valign : 'middle',

				}, {
					title : "基本工资",
					field : 'em_basemoney',
					align : 'center',
					valign : 'middle'

				}, {
					title : '餐补',
					field : 'em_mealmaoney',
					align : 'center',
					valign : 'middle'

				}, {
					title : '住房补助',
					field : 'em_housingmoney',
					align : 'center',
					valign : 'middle'
				}

				, {
					title : '绩效工资',
					field : 'sl_bonus',
					align : 'center',
					valign : 'middle',
					editable : {
						type : "text",
						onblur : "submit",
						showbuttons : true,
						validate : function(v) {
							if (!v || money.test(v) == false)
								return '绩效工资输入非法或为空！';
						}
					},

				}, {
					title : '交通补助',
					field : 'sl_traffic',
					align : 'center',
					valign : 'middle',

					editable : {
						type : "text",
						onblur : "submit",
						showbuttons : true,
						validate : function(v) {
							if (!v || money.test(v) == false)
								return '交通补助输入非法或为空！';
						}
					},

				},

				{
					title : '通讯补助',
					field : 'sl_communication',
					align : 'center',
					valign : 'middle',
					editable : {
						type : "text",
						onblur : "submit",
						showbuttons : true,
						validate : function(v) {
							if (!v || money.test(v) == false)
								return '通讯补助输入非法或为空！';
						}
					},

				},

				{
					title : '出差补助',
					field : 'sl_evection',
					align : 'center',
					valign : 'middle',
					editable : {
						type : "text",
						onblur : "submit",
						showbuttons : true,
						validate : function(v) {
							if (!v || money.test(v) == false)
								return '出差补助输入非法或为空！';
						}
					},
				}, {
					title : '加班补助',
					field : 'sl_overtime',
					align : 'center',
					valign : 'middle',
					editable : {
						type : "text",
						onblur : "submit",
						showbuttons : true,
						validate : function(v) {
							if (!v || money.test(v) == false)
								return '加班补助输入非法或为空！';
						}
					},

				}, {
					title : '其他补助',
					field : 'sl_else',
					align : 'center',
					valign : 'middle',
					editable : {
						type : "text",
						onblur : "submit",
						showbuttons : true,
						validate : function(v) {
							if (!v || money.test(v) == false) {
								return '其他补助输入非法或为空！';
							}
						}
					},

				}, {
					title : '应扣金额',
					field : 'sl_deduct',
					align : 'center',
					valign : 'middle',
					editable : {
						type : "text",
						onblur : "submit",
						showbuttons : true,
						validate : function(v) {
							if (!v || money.test(v) == false)
								return '应扣金额输入非法或为空！';
						}
					},

				}, {
					title : '实发工资',
					field : 'sl_reality',
					align : 'center',
					valign : 'middle',
				} ],
				onEditableSave : function(field, row, oldValue, $el) {
					var rows = {
						index : esIndex, // 更新列所在行的索引
						field : "sl_reality", // 要更新列的field
						value : row.em_basemoney + row.em_mealmaoney
								+ row.em_housingmoney
								+ parseFloat(row.sl_bonus)
								+ parseFloat(row.sl_traffic)
								+ parseFloat(row.sl_communication)
								+ parseFloat(row.sl_evection)
								+ parseFloat(row.sl_overtime)
								+ parseFloat(row.sl_else)
								- parseFloat(row.sl_deduct) // 要更新列的数据
					}// 更新表格数据
					$('#table1').bootstrapTable("updateCell", rows);
				}

			});

	var esIndex;
	$('#table1').on("click-row.bs.table", function(e, row, $element) {
		esIndex = $element.data('index');

	});
	function getPayDate() {
		dnme = wage_detail_name;
		$.ajax({
			type : "GET",
			url : "cp_payoff_index_table.json?wage_detail_name="
					+ wage_detail_name,
			dataType : "json",
			success : function(result) {
				if (result != null) {
					var TableData = result;
					$('#table1').bootstrapTable("load", TableData);
				}
			}
		})
	}

}
function audit() {
	var rows = $('#table1').bootstrapTable('getData');// 行的数据
	var ids = "";
	var payofflist = [];
	for (var i = 0; i < rows.length; i++) {
		if (rows[i].sl_reality == null || rows[i].sl_reality == "") {
			alert("还有未填写的员工工资单！");
			return;
		} else {
			var payoff = {
				"sl_number" : dnme,
				"sl_dgdh" : rows[i].pid,
				"sl_bonus" : rows[i].sl_bonus,
				"sl_traffic" : rows[i].sl_traffic,
				"sl_communication" : rows[i].sl_communication,
				"sl_evection" : rows[i].sl_evection,
				"sl_overtime" : rows[i].sl_overtime,
				"sl_else" : rows[i].sl_else,
				"sl_deduct" : rows[i].sl_deduct,
				"sl_reality" : rows[i].sl_reality
			};
			payofflist.push(payoff);

		}
	}
	$.ajax({
		type : "POST",
		url : "addPayofflist.json",
		data : {
			payoff : JSON.stringify(payofflist)
		},
		dataType : "json",
		success : function(data) {
			if (data.result == true) {
				alert("登记成功！");
				$("#light").css("display", "none");
				$("#fade").css("display", "none");
				getData1();
			} else {
				alert("登记失败！")
			}
		}
	})
}
var dename;
function detail1(wage_detail_name) {
	dename = wage_detail_name;
	$("#light").css("display", "inline");
	$("#fade").css("display", "inline");
	$("#btn").css("display", "none");
    $("#table1").css("display", "none");
    $("#table2").css("display", "inline");
	$
			.ajax({
				type : "GET",
				url : "cp_payoff_index.json?wage_detail_name=" + dename,
				dataType : "json",
				success : function(data) {
					if (data != null) {

						$("#span_wage_detail_name").html(
								data[0].wage_detail_name);
						$("#span_oz_department").html(data[0].oz_department);
						$("#span_countPersonnelfile_id").html(
								data[0].countPersonnelfile_id);
						$("#span_sumEm_totalmoney").html(
								data[0].sumEm_totalmoney);
						
						}
						

					
				}
			})

	$('#table2').bootstrapTable({

		method : "POST",
		url : "getSendPay.json",
		striped : true,
		singleSelect : false,
		dataType : "json",
		search : false, // 显示搜索框
		contentType : "application/x-www-form-urlencoded",
		queryParams : function(params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
			return {
				pageSize : params.limit, // 每页要显示的数据条数
				offset : params.offset, // 每页显示数据的开始行号
				sort : params.sort, // 要排序的字段
				sortOrder : params.order, // 排序规则
				wage_detail_name : dename
			// 额外添加的参数
			}
		},

		columns : [ {
			title : '姓名',
			field : 'pf_name',
			align : 'center'
		}, {
			title : '档案编码',
			field : 'pid',
			align : 'center',
			valign : 'middle',

		}, {
			title : "基本工资",
			field : 'em_basemoney',
			align : 'center',
			valign : 'middle'

		}, {
			title : '餐补',
			field : 'em_mealmaoney',
			align : 'center',
			valign : 'middle'

		}, {
			title : '住房补助',
			field : 'em_housingmoney',
			align : 'center',
			valign : 'middle'
		}

		, {
			title : '绩效工资',
			field : 'sl_bonus',
			align : 'center',
			valign : 'middle',

		}, {
			title : '交通补助',
			field : 'sl_traffic',
			align : 'center',
			valign : 'middle',

		},

		{
			title : '通讯补助',
			field : 'sl_communication',
			align : 'center',
			valign : 'middle',

		},

		{
			title : '出差补助',
			field : 'sl_evection',
			align : 'center',
			valign : 'middle',

		}, {
			title : '加班补助',
			field : 'sl_overtime',
			align : 'center',
			valign : 'middle',

		}, {
			title : '其他补助',
			field : 'sl_else',
			align : 'center',
			valign : 'middle',

		}, {
			title : '应扣金额',
			field : 'sl_deduct',
			align : 'center',
			valign : 'middle',

		}, {
			title : '实发工资',
			field : 'sl_reality',
			align : 'center',
			valign : 'middle',
		} ]
	});
	
	getPay();
	
	
}

function getPay() {

	$.ajax({
		type : "GET",
		url : "getSendPay.json?wage_detail_name=" + dename,
		dataType : "json",
		success : function(data) {
			if (data != null) {
				$('#table2').bootstrapTable("load", data);
				var rows = $('#table2').bootstrapTable('getData');// 行的数据
				var countnum = null;
				for (var i = 0; i < rows.length; i++) {													
					countnum += rows[i].sl_reality;
				}
			   $("#span_sendTotalMoney").html(countnum);
			}
		}
	});
}
